package com.cy.aircompressor.main.home.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;

import com.cy.aircompressor.api.RepositoryImpl;
import com.cy.aircompressor.entity.compressor.Compressor;
import com.cy.aircompressor.entity.maintenance.MaintenanceRecord;
import com.cy.aircompressor.entity.sensor.Sensor;
import com.cy.aircompressor.entity.sensor.SensorLatestData;
import com.cy.aircompressor.entity.sensor.SensorStatisticsData;
import com.cy.aircompressor.entity.warning.WarningMsg;
import com.drug.lib.view.recycptr.bean.RowEntity;
import com.net.model.base.Resource;
import com.net.model.viewmodel.BaseViewModel;

import java.util.List;

/**
 * 版权：周和权 版权所有
 *
 * @author zhouhequan
 * 版本：1.0
 * 创建日期：2021/11/12 or 23:27
 * 描述：---
 */
public class MainContentViewModel extends BaseViewModel<RepositoryImpl> {
    private MediatorLiveData<Resource<RowEntity<Compressor>>> compressorObservable;
    private MediatorLiveData<Resource<List<SensorLatestData>>> queryCompressorSensorDataObservable;
    private MediatorLiveData<Resource<RowEntity<WarningMsg>>> queryWarningByPageObservable;
    private MediatorLiveData<Resource<RowEntity<MaintenanceRecord>>> queryMaintenanceRecordByPageObservable;
    private MediatorLiveData<Resource<RowEntity<Sensor>>> querySensorByPage;
    private MediatorLiveData<Resource<RowEntity<Sensor>>> queryAllSensorByPage;
    private MediatorLiveData<Resource<Integer>> warningObservable;
    private MediatorLiveData<Resource<List<SensorStatisticsData>>> querySensorStatisticsDataDataObservable;
    private MediatorLiveData<Resource<Object>> updatePwdObservable;
    private MediatorLiveData<Resource<Object>> logoutObservable;
    public MainContentViewModel(@NonNull Application application) {
        super(application);
        bindBaseModel(RepositoryImpl.get());
        compressorObservable = new MediatorLiveData<>();
        queryCompressorSensorDataObservable = new MediatorLiveData<>();
        queryWarningByPageObservable = new MediatorLiveData<>();
        queryMaintenanceRecordByPageObservable = new MediatorLiveData<>();
        querySensorByPage = new MediatorLiveData<>();
        queryAllSensorByPage = new MediatorLiveData<>();
        warningObservable = new MediatorLiveData<>();
        querySensorStatisticsDataDataObservable = new MediatorLiveData<>();
        updatePwdObservable = new MediatorLiveData<>();
        logoutObservable = new MediatorLiveData<>();

    }


    public void compressor(String powerStationId) {
        compressorObservable.addSource(baseModel.compressor(1, 50, powerStationId, null), new Observer<Resource<RowEntity<Compressor>>>() {
            @Override
            public void onChanged(Resource<RowEntity<Compressor>> authBeanResource) {
                compressorObservable.setValue(authBeanResource);
            }
        });
    }

    public void queryCompressorSensorRealTimeData(List<String> pram) {
        queryCompressorSensorDataObservable.addSource(baseModel.queryCompressorSensorData(pram), new Observer<Resource<List<SensorLatestData>>>() {
            @Override
            public void onChanged(Resource<List<SensorLatestData>> authBeanResource) {
                queryCompressorSensorDataObservable.setValue(authBeanResource);
            }
        });
    }

    public void queryWarningByPage(String compressorId) {
        queryWarningByPageObservable.addSource(baseModel.queryWarningByPage(1, 20, compressorId), new Observer<Resource<RowEntity<WarningMsg>>>() {
            @Override
            public void onChanged(Resource<RowEntity<WarningMsg>> rowEntityResource) {
                queryWarningByPageObservable.setValue(rowEntityResource);
            }
        });
    }


    public void queryMaintenanceRecordByPage(String compressorId) {
        queryMaintenanceRecordByPageObservable.addSource(baseModel.queryMaintenanceRecordByPage(1, 20, compressorId, null), new Observer<Resource<RowEntity<MaintenanceRecord>>>() {
            @Override
            public void onChanged(Resource<RowEntity<MaintenanceRecord>> rowEntityResource) {
                queryMaintenanceRecordByPageObservable.setValue(rowEntityResource);
            }
        });
    }

    public void querySensorByPage(String compressorId,String powerStationId) {
        querySensorByPage.addSource(baseModel.querySensorByPage(1, 50, compressorId, powerStationId, null, null, null), new Observer<Resource<RowEntity<Sensor>>>() {
            @Override
            public void onChanged(Resource<RowEntity<Sensor>> rowEntityResource) {
                querySensorByPage.setValue(rowEntityResource);
            }
        });
    }

    public void queryAllSensorByPage(String powerStationId) {
        queryAllSensorByPage.addSource(baseModel.querySensorByPage(1, 50, null, powerStationId, null, null, null), new Observer<Resource<RowEntity<Sensor>>>() {
            @Override
            public void onChanged(Resource<RowEntity<Sensor>> rowEntityResource) {
                queryAllSensorByPage.setValue(rowEntityResource);
            }
        });
    }
    public void queryWarningCount() {
        warningObservable.addSource(getRepository().queryWarningCount(), new Observer<Resource<Integer>>() {
            @Override
            public void onChanged(Resource<Integer> sysConfigResource) {
                warningObservable.setValue(sysConfigResource);
            }
        });
    }
    public void querySensorStatisticsData(String startTime, String endTime, String sn, Integer type) {
        querySensorStatisticsDataDataObservable.addSource(baseModel.querySensorStatisticsData(startTime, endTime, "asc", sn, type), new Observer<Resource<List<SensorStatisticsData>>>() {
            @Override
            public void onChanged(Resource<List<SensorStatisticsData>> authBeanResource) {
                querySensorStatisticsDataDataObservable.setValue(authBeanResource);
            }
        });
    }

    public void updatePwd(Object parameters) {
        updatePwdObservable.addSource(getRepository().updatePwd(parameters), new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> userInfoResource) {
                updatePwdObservable.setValue(userInfoResource);
            }
        });
    }
    public void logout() {
        logoutObservable.addSource(getRepository().logout(), new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> userInfoResource) {
                logoutObservable.setValue(userInfoResource);
            }
        });
    }
    public MediatorLiveData<Resource<Integer>> getWarningObservable() {
        return warningObservable;
    }

    public MediatorLiveData<Resource<RowEntity<Compressor>>> getCompressorObservable() {
        return compressorObservable;
    }

    public MediatorLiveData<Resource<List<SensorLatestData>>> getQueryCompressorSensorDataObservable() {
        return queryCompressorSensorDataObservable;
    }

    public MediatorLiveData<Resource<RowEntity<WarningMsg>>> getQueryWarningByPageObservable() {
        return queryWarningByPageObservable;
    }

    public MediatorLiveData<Resource<RowEntity<MaintenanceRecord>>> getQueryMaintenanceRecordByPageObservable() {
        return queryMaintenanceRecordByPageObservable;
    }

    public MediatorLiveData<Resource<RowEntity<Sensor>>> getQuerySensorByPage() {
        return querySensorByPage;
    }

    public MediatorLiveData<Resource<RowEntity<Sensor>>> getQueryAllSensorByPage() {
        return queryAllSensorByPage;
    }

    public MediatorLiveData<Resource<List<SensorStatisticsData>>> getQuerySensorStatisticsDataDataObservable() {
        return querySensorStatisticsDataDataObservable;
    }

    public MediatorLiveData<Resource<Object>> getUpdatePwdObservable() {
        return updatePwdObservable;
    }

    public MediatorLiveData<Resource<Object>> getLogoutObservable() {
        return logoutObservable;
    }
}
